﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Collections.Generic;
using Coda.Nth.DbConnect;
using System.Data;
using System.ComponentModel.Composition;



namespace Coda.Nth.BusinessLogic
{
     [Export(typeof(IBusinessFacade))]
     public  class BusinessFacade : IBusinessFacade  
    {
        public IEnumerable<Price> GetPrices()
        {
            using (var context = new NthDbConnectContainer())
            {


                var price = context.PriceSet.Select(x => x).ToList<Price>();
                return price;
            }
        }

        
        public IEnumerable<City> GetCities()
        {
            using (var context = new NthDbConnectContainer())
            {
                var cities = context.CitySet.Select(x => x).ToList();
                cities.ForEach(x => context.Detach(x));
                return cities;
            }
        }

        
        public IEnumerable<Country> GetCountries()
        {
            using (var context = new NthDbConnectContainer())
            {

                var countries = context.CountrySet.Select(x => x).ToList();
                countries.ForEach(x => context.Detach(x));
                return countries;
            }
        }

       
        //public IEnumerable<DaoManagerFee> GetManagerFees()
        //{
        //    using (var context = new NthDbConnectContainer())
        //    {
        //        var fees = from manager in context.FeeSet.OfType<ManagerFee>()
        //                   select new DaoManagerFee { Amount = manager.Amount, Id = manager.Id, Manger = manager.Manger };


        //        return fees.ToList();
        //    }
        //}

      
       
        public void PutPortfolios(IEnumerable<Portfolio> portfolios)
        {
            using (var context = new NthDbConnectContainer())
            {
                portfolios.Where(x => x.Id != 0).ToList().ForEach(y =>
                {
                    context.Attach(y);
                    context.ObjectStateManager.ChangeObjectState(y, EntityState.Modified);
                });
                portfolios.Where(y => y.Id == 0).ToList().ForEach(x => context.AddToPortfolioSet(x));
                context.SaveChanges();
            }
        }

        
        public IEnumerable<Portfolio> GetPortfolios()
        {
            using (var context = new NthDbConnectContainer())
            {
                var pr = context.PortfolioSet.Select(x => x).ToList();
                pr.ForEach(y => context.Detach(y));
                return pr;
            }
        }
    }
}
